백준 11728번 배열 합치기

Solution

  • 배열 초기화 수를 잘못 입력해서 엄청 헤맸다. 뭔가 잘 안 될 때는 입력부터 다시 보자.
import java.io.*;
import java.util.*;

public class Main {
    static StringBuilder sb = new StringBuilder();
    static FastReader scan = new FastReader();

    static int n, m;
    static int[] a, b;

    static void input() {
        n = scan.nextInt();
        m = scan.nextInt();
        a = new int[n];
        b = new int[m];

        for (int i = 0; i < n; i++) {
            a[i] = scan.nextInt();
        }

        for (int i = 0; i < m; i++) {
            b[i] = scan.nextInt();
        }
    }

    static void pro() {
        int ap = 0, bp = 0;

        while (ap < n && bp < m) {
            if (a[ap] < b[bp]) {
                sb.append(a[ap++]).append(" ");
            } else {
                sb.append(b[bp++]).append(" ");
            }
        }

        while (ap < n) {
            sb.append(a[ap++]).append(" ");
        }

        while (bp < m) {
            sb.append(b[bp++]).append(" ");
        }

        System.out.println(sb);
    }

    public static void main(String[] args) {
        input();
        pro();
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public FastReader(String s) throws FileNotFoundException {
            br = new BufferedReader(new FileReader(new File(s)));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}